package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.OrganizationRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Organization;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by elvisxiong on 7/8/15.
 */
@Service
public class OrganizationRdbServiceImpl extends DaoTemplate implements OrganizationRdbService {
    @Override
    public List<Organization> getOrganizationByFbAccountId(Long fbAccountId) {
        String sql = "SELECT" +
                "		DISTINCT org.id, org.`name`, org.status" +
                "	FROM" +
                "		`user` u," +
                "    organization org," +
                "		fb_userpermissions_adaccounts fua," +
                "		fb_user fuser," +
                "		entrust_account ea" +
                "	WHERE" +
                "		u.ticket = fuser.user_id" +
                "	AND fua.fb_user_id = fuser.fb_user_id" +
                "	AND ea.fb_account_id = fua.fb_account_id" +
                "  and org.id = u.org_id" +
                "  and ea.fb_account_id = ? " +
                "  ORDER BY org.id";
        return getJdbcTemplate().query(sql, new Object[]{fbAccountId}, DefaultRowMapper.autoRowMapper(Organization.class));
    }

    @Override
    public Long save(Organization organization) {
        String sql = "INSERT INTO `organization` (" +
                "  `name`," +
                "  `link_man`," +
                "  `link_tel`," +
                "  `email`," +
                "  `address`," +
                "  `status`" +
                ")  VALUES (?,?,?,?,?,?) ;";
        Long flag = saveResultId(sql, new PstAssign() {
            @Override
            public void setParam(PreparedStatement ps) throws SQLException {
                ps.setObject(1, organization.getName());
                ps.setObject(2, organization.getLinkMan());
                ps.setObject(3, organization.getLinkTel());
                ps.setObject(4, organization.getEmail());
                ps.setObject(5, organization.getAddress());
                ps.setObject(6, 1);
            }
        });
        return flag;
    }

    @Override
    public Organization findById(Long id) {
        String sql = "SELECT * FROM organization t WHERE t.id = ?";
        List<Organization> list;
        list = getJdbcTemplate().query(sql, new Object[]{id}, DefaultRowMapper.autoRowMapper
                (Organization.class));
        if(list != null && list.size() > 0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public void modify(Organization organization) {
        String sql = "UPDATE  organization t SET `name` = ?,`link_man` =?,`link_tel` = ?,  `email` =? , " +
                " `address`=?,  `status`=? WHERE t.id = ? ;";
        save(sql, new PstAssign() {
            @Override
            public void setParam(PreparedStatement ps) throws SQLException {
                ps.setObject(1, organization.getName());
                ps.setObject(2, organization.getLinkMan());
                ps.setObject(3, organization.getLinkTel());
                ps.setObject(4, organization.getEmail());
                ps.setObject(5, organization.getAddress());
                ps.setObject(6, organization.getStatus());
                ps.setObject(7, organization.getId());
            }
        });
    }
}
